import Toast from 'tdesign-miniprogram/toast/index';
var QRCode = require('../../../utils/weapp-qrcodenew');

Page({
  data: {
    userInfo: {
    },
    partnerCode: '',
    isEnable: false,
    qrCodeVisible: false
  },

  onLoad() {

  },

  onShow() {
    var userInfo = getApp().globalData.admin;
    userInfo.user.name = userInfo.user.nickname;
    this.setData({
      userInfo: userInfo.user,
      partnerCode: userInfo.partnerCode
    })
  },

  logOut() {
    wx.clearStorageSync("admin_token")
    wx.redirectTo({
      url: '/pages/admin/login/index',
    })
  },

  onClickCell({
    currentTarget
  }) {
    const {
      type
    } = currentTarget.dataset;

    switch (type) {
      case 'carlist': {
        wx.navigateTo({
          url: '/pages/admin/usercenter/carlist/index'
        });
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      case 'qrcode': {
        this.showQrCode();
        break;
      }
      case 'dealers': {
        wx.navigateTo({
          url: '/pages/admin/usercenter/business/index'
        });
        break;
      }
      case 'addcar': {
        wx.navigateTo({
          url: '/pages/admin/usercenter/editcar/index'
        });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '开发中',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  showQrCode() {
    var url = getApp().globalData.qrUrl + "?code=" + this.data.partnerCode
    const systemInfo = wx.getSystemInfoSync()
    this.setData({
      width: 200 * systemInfo.windowWidth / 375,
      height: 200 * systemInfo.windowWidth / 375,
    })
    console.log(200 * systemInfo.windowWidth / 375);
    new QRCode('myQrcode', {
      text: url, //此处内容可以是url可以是string类型
      width: 180 * systemInfo.windowWidth / 375,
      height: 180 * systemInfo.windowWidth / 375,
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      callback: (res) => {
        console.log(res.path)
        wx.saveImageToPhotosAlbum({ // 保存二维码到本地
          filePath: res.path, // 通过dom获取二维码的临时地址
          success() {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              wx.showToast({
                title: '请前往设置页打开权限',
                duration: 1000,
                icon: 'none',
                success: () => {
                  that.setData({
                    seeting: true
                  })
                }
              })
            }
          }
        })
      }
    })
    this.setData({ qrCodeVisible: true })
  },

  openMakePhone() {
    this.setData({
      showMakePhone: true
    });
  },

  closeMakePhone() {
    this.setData({
      showMakePhone: false
    });
  },

  closeQrCode() {
    this.setData({ qrCodeVisible: false })
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: getApp().globalData.servicePhone,
    });
  },

  gotoUserEditPage() {
    const {
      currAuthStep
    } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({
        url: '/pages/usercenter/person-info/index'
      });
    } else {
      this.fetUseriInfoHandle();
    }
  },


});